package com.esd.crm.util;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * 邮件收发处理类
 * 
 * @author yufu
 * @email feimeideyanggao@126.com
 * @create 2018年11月7日
 */
public class MailUtils {

	private static Logger logger = Logger.getLogger(MailUtils.class);
	private static String host = "smtp.163.com";

	/**
	 * 使用此邮箱
	 */
//	private static String user = "supertestabc@163.com";
//	private static String pwd = "abc123";
//	private static String from = "supertestabc@163.com";

	// 使用esd陈超手机注册的163邮箱账号
	private static String user = "esdkhgl@163.com";
	private static String pwd = "esd123456";
	private static String from = "esdkhgl@163.com";
	

	// private String to = "254043921@qq.com";
	private static String subjectUserName = "亿时代客户管理信息";

	/**
	 * 向目标邮箱发送邮件
	 * @author yufu
	 * @email feimeideyanggao@126.com
	 * @create 2018年11月7日
	 * @param targetEmail 接收邮箱
	 * @param emailContent 邮件内容, 支持html
	 * @return
	 */
	public static boolean send(String targetEmail, String emailContent) {
		if (StringUtils.isBlank(targetEmail) || StringUtils.isBlank(emailContent)) {
			logger.warn("参数为空! targetEmail: " + targetEmail + ", emailContent:" + emailContent);
			return false;
		}
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		props.put("mail.transport.protocol", "smtp");

		// ECS基于安全考虑，目前已禁用25端口。
		// 如果您的发送程序部署在阿里云ECS上，建议您不勾选SSL时，使用80端口，勾选SSL时，使用465端口。
		// 测试端口 telnet smtp.163.com 25
		// 测试网络 ping smtp.163.com
		// 如果使用ssl，则去掉使用25端口的配置，进行如下配置,
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.port", "465");

		Session session = Session.getInstance(props);
		session.setDebug(false);

		// 创建一个邮件对象 message
		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(targetEmail));
			// 整封邮件
			MimeMultipart bodyMultipart = new MimeMultipart();

			// 邮件主题显示的内容
			MimeBodyPart bodyPart = new MimeBodyPart();
			// MimeBodyPart attach1 = new MimeBodyPart();
			bodyMultipart.addBodyPart(bodyPart);
			// bodyMultipart.addBodyPart(attach1);
			// body内的内容
			message.setSubject(subjectUserName);
			emailContent += "<br/>此邮件为系统自动发送，请不要回复。";
			bodyPart.setContent(emailContent, "text/html;charset=UTF-8");

			// 将body的内容保存到邮件内
			message.setContent(bodyMultipart);
			// 保存更改信息

			// // 给附件attach1附加上文件
			// DataSource ds = new FileDataSource("d:\\10000000.png");
			// DataHandler dh = new DataHandler(ds);
			// attach1.setDataHandler(dh);
			// try {
			// attach1.setFileName(MimeUtility.encodeText("10000000.png"));
			// } catch (UnsupportedEncodingException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			// message.saveChanges();

			// 传输对象
			Transport transport = session.getTransport("smtp");
			// 建立连接
			transport.connect(host, user, pwd);
			// 发送邮件
			transport.sendMessage(message, message.getAllRecipients());
			// 关闭传输
			transport.close();
			logger.info("邮件发送成功：" + targetEmail + ", emailContent:" + emailContent);
			return true;
		} catch (AddressException e) {
			logger.error(e.getMessage());
			return false;
		} catch (MessagingException e) {
			logger.error(e.getMessage());
			return false;
		}

	}

	public static void main(String[] args) {
		MailUtils.send("1148522034@qq.com", "哈哈哈");
	}

}